﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using Context;
namespace WCF_ListenMusic.Services.Module_Song.Public
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SongService" in code, svc and config file together.
    public class SongService : ISongService
    {
        Context.Connect_DBDataContext context = new Context.Connect_DBDataContext();
        public List<View_BXH_BaiHat> getBXH_BaiHat()
        {
            try
            {
                var q = from a in context.View_BXH_BaiHats
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public Song getSong_byID(int songid)
        {
            try
            {
                var q = from a in context.Songs
                        where a.SongID.Equals(songid)
                        select a;
                return q.Single();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        public List<Song> getAllSong()
        {
            try
            {
                var q = from a in context.Songs
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<SongAlbum> getSong_byAlbumID(int albumid)
        {
            try
            {
                if (albumid <= 0)
                {
                    throw new FaultException("Lỗi Album ID nhỏ hơn hoặc bằng 0");
                }
                var q = from a in context.SongAlbums
                        where a.AlbumID.Equals(albumid)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        public List<Song> get10song_bySingerid(int singerid)
        {
            try
            {
                var q = (from a in context.Songs
                         where a.SingerID.Equals(singerid)
                         select a).Take(10);
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        public int countSong_bySingerid(int singerid)
        {
            try
            {
                var q = (from a in context.SongSingers
                         where a.SingerID.Equals(singerid)
                         select a).Count();
                return q;
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<SongSinger> getRandom10Song_byCategoryid(int categoryid)
        {

            try
            {
                var q = (from a in context.SongSingers
                         where a.CategoryID.Equals(categoryid) && a.TypeID.Equals(1) && a.Active.Equals(true)
                         orderby context.GetRandomInt()
                         select a).Take(10);
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<View20_Random_BaiBat> get20Random_Song()
        {
            try
            {
                var q = from a in context.View20_Random_BaiBats
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<ViewSearch_SongName> search_SongName(string keyword)
        {
            try
            {
                var q = from a in context.ViewSearch_SongNames
                        join b in context.search_SongName(keyword) on a.SongID equals b.KEY
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        public int countSong_byCategoryID(int categoryid)
        {
            try
            {

                var q = (from a in context.CategorySongs
                         where a.CategoryID.Equals(categoryid)
                         select a).Count();
                return q;
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        #region ISongService Members


        public List<SongAlbum> getAllSongInAlbum()
        {

            try
            {

                var q = from a in context.SongAlbums
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        #endregion


        public List<CountrySong> getRangeSong_Video_byTypeid(int typeid, int countryid)
        {
            try
            {
                var q = (from a in context.CountrySongs
                         where a.TypeID.Equals(typeid) && a.CountryID.Equals(countryid)
                         orderby a.CountView descending
                         select a).Take(30);
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<Entities.Type> getAllType()
        {
            try
            {
                var q = from a in context.Types
                        where !a.TypeName.Contains("Karaoke")
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<Album_Song> getSonginAlbum(int songid)
        {
            try
            {
                var q = from a in context.Album_Songs
                        where a.SongID.Equals(songid)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<SongSinger> getSong_bySongID(int songid)
        {
            try
            {
                var q = from a in context.SongSingers
                        where a.SongID.Equals(songid) && a.Active.Equals(true)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        public List<CategorySong> getSongbyCategoryID(int categoryid)
        {
            try
            {
                var q = from a in context.CategorySongs
                        where a.CategoryID.Equals(categoryid)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        public List<SongSinger> getSongbySingerID(int singerid, int typeid)
        {
            try
            {
                var q = from a in context.SongSingers
                        where a.SingerID.Equals(singerid) && a.TypeID.Equals(typeid) && a.Active.Equals(true)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public bool updateCountView(int songid)
        {
            try
            {
                var q = from a in context.Songs
                        where a.SongID.Equals(songid)
                        select a;
                Song s = q.Single();
                s.CountView += 1;
                context.SubmitChanges();
                return true;
            }
            catch (Exception ex) { return false; throw new FaultException(ex.Message); }
        }


        public List<SongSinger> getSongbySingerName(string singerName, int typeid)
        {
            try {
                var q = from a in context.SongSingers
                        where a.SingerName.Trim().ToLower().Equals(singerName.Trim().ToLower()) && a.TypeID.Equals(typeid) && a.Active.Equals(true)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<SongSinger> getAllSong_bySongName(string songname, int typeid)
        {
            try
            {
                var q = from a in context.SongSingers
                        where a.SongName.Trim().ToLower().Contains(songname.Trim().ToLower()) && a.TypeID.Equals(typeid) && a.Active.Equals(true)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }
    }
}
